package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import bean.User;
import service.UserService;
import service.impl.UserServiceImpl;

@WebServlet("/user/login")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.login(username, password);
        if (user != null) {
            request.getSession().setAttribute("user",user);
            request.setAttribute("message","登录成功");
            request.setAttribute("page","index.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        } else {
            request.setAttribute("message","登录失败");
            request.setAttribute("page","login.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }
    }
}
